#!/bin/bash

#------------------------------------------------------------------------------
#Filename:	75335-nfs.sh
#Version:	1.1
#Date:		2013/11/18
#Author:	yuanhui.shi
#Email:		yuanhui.shi@cs2c.com.cn
#Summary:	02.04网络服务-05NFS-01字符模式-02添加共享目录-02添加主机为指定网段中所有主机
#Notes:         NFS配置——添加主机为指定网段中所有主机
#Copyright:	China Standard Software Co.,Ltd.
#History:
#		Version 1.0,2013/11/19
#		-The first one
#-----------------------------------------------------------------------------


PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../../lib/Echo.sh
source ./lib/Ssh.sh
source ../../../lib/XmlParse.sh
source ./lib/cmd.sh

function CleanData ()
{
  umount /mnt/rw &> /dev/null
  umount /mnt/ro &> /dev/null
  EchoInfo "关闭服务器端的nfs服务，删除共享目录"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "command -v systemctl && /bin/systemctl stop nfs.service || /sbin/service nfs stop"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "rm -rf /nfs-test"
  Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "echo '' >/etc/exports"
#  Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "umount /mnt/rw"
#  Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "umount /mnt/ro"
#  Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "rm -rf /mnt/*"
  rm -rf  /mnt/*
  EchoInfo "75335-nfs.sh执行完毕"
}

trap "CleanData" EXIT INT

if [ -z "$SFROOT" ]
then
    echo "SFROOT is null"
    exit 1
fi

DATA_DIR="${SFROOT}/testcases/Network_Service"
DATA_FILE="config.xml"

XmlParse ${DATA_DIR}/${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

USER=${RUSERNAME}
ServerIP=${REHOST}
PASSWORD=${PASSWORD}

LOCAL_IP=`ifconfig | grep 'inet ' | grep -m 1 -v '127.0.0.1'  | awk '{print $2}' | tr -d "addr:"`
NETID_3=`echo ${LOCAL_IP} | awk 'BEGIN{OFS=FS="."}{print $3}'`
NETID_3=$((NETID_3+1))
NETID_2=`echo ${LOCAL_IP} | awk 'BEGIN{OFS=FS="."}{print $1,$2}'`
AuthIPS="${NETID_2}.${NETID_3}.*"

#DATA_DIR="./data"
#DATA_FILE="75335-nfs.xml"

#XmlParse ${DATA_DIR}/${DATA_FILE}

#GetValue USER USER
#GetValue ServerIP ServerIP
#GetValue PASSWORD PASSWORD
#GetValue AuthUSER AuthUSER
#GetValue AuthIP AuthIP
#GetValue AuthPASSWORD AuthPASSWORD
#GetValue AuthIPS  AuthIPS

#远程配置服务器
EchoInfo "远程配置服务器${ServerIP}"

Scp "${USER}" "${ServerIP}" "${PASSWORD}" "./nfs-config.sh" "/root/"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "sed -i s/*/${AuthIPS}/g /root/nfs-config.sh"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "sh nfs-config.sh"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}" "echo 查看服务器端nfs共享目录清单"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}"  "showmount -e 127.0.0.1"

Ssh "${USER}" "${ServerIP}" "${PASSWORD}"  "rm -rf /root/nfs-config.sh"

#客户端是否可以查看到nfs共享目录
EchoInfo "客户端是否可以查看nfs共享目录"
stop_daemon iptables  

test -n "`showmount -e ${ServerIP} |grep /nfs-test/readonly`" -a -n "`showmount -e ${ServerIP} |grep /nfs-test/rw`" 
EchoResult "客户端可以看见nfs共享目录"

#---------------------------------授权客户端------------------------------------
#判断是否为授权客户端
#EchoInfo "判断客户端是否为授权客户端"
#Var1=`echo ${AuthIP} |cut -d . -f 1-3`
#Var2=`echo ${AuthIPS} |cut -d . -f 1-3`
#
#if [ "${Var1}" != "${Var2}" ]
#then 
#    echo "请设定一个授权网段的客户端"
#    exit 1
#fi
#
##授权客户端在/mnt目录下新建两个目录：rw和ro
#EchoInfo "授权客户端新建目录"
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mkdir /mnt/ro"
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mkdir /mnt/rw"
#
#
##授权客户端挂载只读目录/nfs-test/readonly
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mount ${ServerIP}:/nfs-test/readonly /mnt/ro"
#EchoResult "授权客户端可以挂载只读目录" 
#
##授权客户端挂载读写目录/nfs-test/rw
#Ssh "${AuthUSER}" "${AuthIP}" "${AuthPASSWORD}" "mount ${ServerIP}:/nfs-test/rw /mnt/rw"
#EchoResult "授权客户端可以挂载读写目录"

#--------------------------------非授权客户端-----------------------------------
#判断本地客户端属性
EchoInfo "判断本地客户端属性"
#LOCALIP=`ifconfig eth0 |grep "inet addr" |sed s/^.*addr://g |sed s/Bcast.*$//g`
Var2=`echo ${AuthIPS} |cut -d . -f 1-3`
Var3=`echo ${LOCALIP} |cut -d . -f 1-3`

if [ "${Var3}" == "${Var2}" ]
then 
    echo "请设定一个非授权网段的客户端"
    exit 1
fi

#非授权客户端在/mnt目录下新建两个目录：rw和ro
EchoInfo "非授权客户端新建目录"
test -d /mnt/rw  || mkdir /mnt/rw
test -d /mnt/ro  || mkdir /mnt/ro

#mkdir /mnt/ro
#mkdir /mnt/rw

#非授权客户端挂载只读目录/nfs-test/readonly
mount ${ServerIP}:/nfs-test/readonly /mnt/ro &>/dev/null

if [ $? -ne 0 ];then
    echo "PASS|非授权客户端不能挂载只读目录"
else
    echo "FAIL|非授权客户端不能挂载只读目录"
fi

#非授权客户端挂载读写目录/nfs-test/rw
mount ${ServerIP}:/nfs-test/rw /mnt/rw &>/dev/null

if [ $? -ne 0 ];then
    echo "PASS|非授权客户端不能挂载读写目录"
else
    echo "FAIL|非授权客户端不能挂载读写目录"
fi
